package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.ResumeInfo;
import com.baomidou.ant.service.IResumeInfoService;
import com.baomidou.ant.utils.MyFileUtil;
import com.baomidou.ant.vo.request.RequestListResumeVo;
import com.baomidou.ant.vo.response.ResponseListResumeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/resume-info")
public class ResumeInfoController {
    @Autowired
    IResumeInfoService resumeInfoService;
    @Value("${file.pathName}")
    private String path;

    //文件上传接口
    @RequestMapping("/sendResume")
    @ResponseBody
    public Result uploadFile(@RequestParam MultipartFile file, HttpServletRequest request) throws Exception  {
        //得到前端传来的参数
        String userName=request.getParameter("userName");
        String userTel=request.getParameter("userTel");
        String intentionPost=request.getParameter("intentionPost");
        if(ObjectUtils.isNotEmpty(resumeInfoService.queryByUser(userName,userTel))){
            return Result.getFailure().setMsg("检测到您已经提交过简历！禁止重复提交！");
        }else {
            String fileName;
            fileName=userName+"-"+userTel+"-简历附件";
            String issueAffix = null;
            if (file != null && !file.isEmpty()){
                if ((issueAffix = MyFileUtil.handleFormUpload(file,fileName,path)) != null) {
                    ResumeInfo resumeInfo=new ResumeInfo();
                    resumeInfo.setApplyUserName(userName);
                    resumeInfo.setResumePath("ResumeFile/"+fileName+".pdf");
                    resumeInfo.setUserTel(userTel);
                    resumeInfo.setIntentionPost(intentionPost);
                    resumeInfoService.save(resumeInfo);
                    return Result.getSuccess().setMsg("简历投递成功!");
                } else if(Objects.equals(MyFileUtil.handleFormUpload(file,fileName,path), "exist")) {
                    System.err.println("简历上传失败!");
                    return Result.getFailure().setMsg("简历上传失败,重复提交!");
                }else{
                    System.err.println("文件,上传失败!");
                    return Result.getFailure().setMsg("上传失败!");
                }
            }else
                return Result.getFailure().setMsg("文件为空");
        }
    }

    //分页查询
    @PostMapping("/listResume")
    public Result listResume(@RequestBody RequestListResumeVo vo){
        IPage<ResponseListResumeVo> iPage=resumeInfoService.listResume(vo);
        return Result.getSuccess().setData(iPage);
    }

}
